import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:hinge/pages/encyclopedia/encyclopedia_page.dart';
import 'package:hinge/pages/encyclopedia/video_detail_page.dart';
import 'package:hinge/pages/main/main_page.dart';
import 'package:hinge/pages/main/splash_page.dart';
import 'package:hinge/pages/message/message_page.dart';
import 'package:hinge/pages/message/topic_page.dart';
import 'package:hinge/pages/note/note_page.dart';
import 'package:hinge/pages/note/release_page.dart';
import 'package:hinge/pages/recommend/detail_page.dart';
import 'package:hinge/pages/recommend/recommend_page.dart';
import 'package:hinge/pages/system/login_page.dart';
import 'package:hinge/pages/system/register_page.dart';
import 'package:hinge/pages/my/personal_page.dart';
import 'package:hinge/pages/my/setup_page.dart';
import 'package:hinge/pages/chat/chat_page.dart';
import 'package:hinge/pages/chat/chat_list_page.dart';
import 'package:hinge/pages/ai/ai_chat_page.dart';

class AppRouter {
  static final _rootNavigatorKey = GlobalKey<NavigatorState>();
  static final _shellNavigatorKey = GlobalKey<NavigatorState>();

  // 全局路由观察者
  static final RouteObserver<ModalRoute<void>> routeObserver =
      RouteObserver<ModalRoute<void>>();

  static final GoRouter router = GoRouter(
    initialLocation: "/splash",
    debugLogDiagnostics: true,
    navigatorKey: _rootNavigatorKey,
    observers: [routeObserver],
    routes: [
      // 设置页面路由
      GoRoute(
        path: "/setup",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) => const SetupPage(),
      ),

      // 启动页路由
      GoRoute(path: "/splash", builder: (context, state) => const SplashPage()),

      // 登录页路由
      GoRoute(
        path: "/login",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) => const LoginPage(),
      ),

      // 注册页路由
      GoRoute(
        path: "/register",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) => const RegisterPage(),
      ),

      // 详情页路由
      GoRoute(
        path: "/detail/:perfumeId",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) {
          final perfumeId = state.pathParameters['perfumeId']!;
          return DetailPage(perfumeId: perfumeId);
        },
      ),

      // 视频详情页路由
      GoRoute(
        path: "/video_detail/:videoId",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) {
          final videoId = state.pathParameters['videoId']!;
          return VideoDetailPage(videoId: videoId);
        },
      ),

      // 个人中心页路由
      GoRoute(
        path: "/personal",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) => const PersonalPage(),
      ),

      // 添加带用户ID参数的个人中心路由
      GoRoute(
        path: "/personal/:userId",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) {
          final userId = int.parse(state.pathParameters['userId']!);
          return PersonalPage(userId: userId);
        },
      ),

      // 聊天页路由
      GoRoute(
        path: "/chat/:chatId",
        parentNavigatorKey: _rootNavigatorKey,
        builder:
            (context, state) =>
                ChatPage(chatId: state.pathParameters['chatId']!),
      ),

      // 聊天列表页路由
      GoRoute(
        path: "/chats",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) => const ChatListPage(),
      ),

      // 主页面路由，包含底部导航栏
      ShellRoute(
        navigatorKey: _shellNavigatorKey,
        builder: (context, state, child) => MainPage(child: child),
        routes: [
          // 各个Tab页面的子路由
          GoRoute(
            path: "/",
            builder:
                (context, state) => RecommendPage(key: RecommendPage.globalKey),
          ),
          GoRoute(path: "/note", builder: (context, state) => const NotePage()),
          GoRoute(
            path: "/message",
            builder: (context, state) => const MessagePage(),
          ),
          GoRoute(
            path: "/encyclopedia",
            builder: (context, state) => const EncyclopediaPage(),
          ),
        ],
      ),

      // 话题详情页路由
      GoRoute(
        path: "/topic/:topicId",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) {
          final topicId = int.parse(state.pathParameters['topicId']!);
          return TopicPage(topicId: topicId);
        },
      ),

      // 发布页面路由
      GoRoute(
        path: "/release",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) => const ReleasePage(),
      ),

      // AI聊天页面路由
      GoRoute(
        path: "/ai_chat",
        parentNavigatorKey: _rootNavigatorKey,
        builder: (context, state) {
          // 获取初始消息，优先使用extra，如果没有则使用空字符串
          final initialMessage =
              state.extra != null ? state.extra as String : '';
          return AIChatPage(initialMessage: initialMessage);
        },
      ),

      // 其他独立页面的路由
    ],
  );
}
